package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleCommentService;
import com.heima.article.service.ArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmCommentDto;
import com.heima.model.wemedia.vos.WmCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ArticleCommentServiceImpl implements ArticleCommentService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    /**
     * 根据（自媒体）用户id查询文章信息 todo
     *
     * @param userId
     * @return
     */
    @Override
    public PageResponseResult getArticleInfo(Integer userId, WmCommentDto dto) {
       /* //0、准备分页参数
        IPage<ApArticle> page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApArticle> lqw = new LambdaQueryWrapper<>();
        //时间倒序
        lqw.orderByDesc(ApArticle::getPublishTime);
        //根据作者（自媒体）id
        lqw.eq(ApArticle::getAuthorId, userId);
        //将字符串时间转为时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//2012-12-12
        try {
            lqw.between(ApArticle::getPublishTime,sdf.parse(dto.getBeginDate()),sdf.parse(dto.getEndDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //1、分页条件查询文章表 得出该用户（自媒体）所有的文章
        articleService.page(page, lqw);
        System.out.println(page.getRecords() + "222222222222232323");
        //2、查询文章是否可以评论 todo 可多表优化
        List<WmCommentVo> list = new ArrayList<>();
        for (ApArticle apArticle : page.getRecords()) {
            WmCommentVo vo = new WmCommentVo();
            //每次循环，都通过文章id找到对应的文章配置
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, apArticle.getId()));
            if (apArticleConfig != null) {
                //评论状态
                vo.setIsComment(apArticleConfig.getIsComment());
                //标题
                vo.setTitle(apArticle.getTitle());
                //评论数
                vo.setComments(apArticle.getComment());
                list.add(vo);
            }
        }
        //创建对象返回
        PageResponseResult res = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        res.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        res.setData(list);
        return res;*/
        //返回用
        Integer page = dto.getPage();
        //准备分页参数
        dto.setPage((dto.getPage() - 1) * dto.getSize());
        //条件分页查询
        List<WmCommentVo> newsComments = apArticleMapper.findNewsComments(userId, dto);
        //如果评论为null的话，全部设为0
        newsComments = newsComments.stream().peek(nc -> {
            if (nc.getComments() == null) nc.setComments(0);
        }).collect(Collectors.toList());
        //查询总数
        int count = apArticleMapper.count(userId, dto);
        PageResponseResult res = new PageResponseResult(page, dto.getSize(), count);
        res.setData(newsComments);
        res.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        return res;
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse("2012-05-12");
        System.out.println(parse);
    }
}
